package cn.qihang.ssm.controller;

import cn.qihang.ssm.pojo.Traveller;
import cn.qihang.ssm.service.TravellerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: qihang
 * @Date: 2022/11/2 17:38
 * @Desc:
 */
@Controller
@RequestMapping("traveller")
public class TravellerContorller {

    @Autowired
    private TravellerService travellerService;

    /**
     * 查询所有旅客
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "size",defaultValue = "5") Integer size){

        PageHelper.startPage(page,size);

        List<Traveller> travellerList =  this.travellerService.findAll();

        PageInfo pageInfo = new PageInfo(travellerList);

        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("traveller-list");
        return mv;
    }

    /**
     * 游客详情
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        Traveller traveller = this.travellerService.findById(id);
        modelAndView.addObject("traveller",traveller);
        modelAndView.setViewName("traveller-show");
        return modelAndView;
    }


    /**
     * 新增游客
     * @param traveller
     * @return
     */
    @RequestMapping("save")
    public String save(Traveller traveller) {
        this.travellerService.save(traveller);
        return "redirect:findAll.do";
    }


    /**
     * 修改游客前的数据回显
     * @param id
     * @return
     */
    @RequestMapping("editById")
    public ModelAndView editById(@RequestParam("id") Integer id){
        ModelAndView modelAndView = new ModelAndView();
        Traveller traveller = this.travellerService.findById(id);
        modelAndView.addObject("traveller",traveller);
        modelAndView.setViewName("traveller-edit");
        return modelAndView;
    }


    /**
     * 修改游客信息
     * @param traveller
     * @return
     */
    @RequestMapping("edit")
    public String edit(Traveller traveller){
        this.travellerService.edit(traveller);
        return "redirect:findAll.do";
    }


    /**
     * 指定删除游客
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public String deleteById(@RequestParam(name = "id") Integer id){
        this.travellerService.deleteById(id);
        return "redirect:findAll.do";
    }

    /**
     * 批量删除
     * @param request
     * @return
     */
    @RequestMapping("selectDelete")
    public String selectDelete(HttpServletRequest request){
        String ids = request.getParameter("ids");

        String[] split = ids.split(",");
        for (String s : split) {
            this.travellerService.deleteById(Integer.parseInt(s));
        }
        return "redirect:findAll.do";
    }
}
